package com.Packet 
{
	import flash.utils.ByteArray;
	
	public class Packet
	{
		public static const SYSTEM:int = -1;
		public static const LOGIC:int = 0;
		
		public var type:int;
		public var id:int;
		public var body:Array;
		
		public function Packet(t:int = 0, i:int = 0, b:Array = null)
		{
			id = i;
			type = t;
			body = b?b:[];
			
		}
		
		public function add(value:*):void
		{
			if((value is String) || (value is int) || (value is Number) || (value is ByteArray))
			{
				body.push(value);
			}
			else
			{
				throw new Error("Packet add 传入的数据类型错误");
			}
		}
		
		public function toString():String
		{
			return " id: " + id + " type: " + type +" body: " + body; 
		}
		
		public static function encode(o:Packet):ByteArray 
		{
			var encoder:PacketEncoder = new PacketEncoder(o);
			return encoder.getByte();
		}
		
		public static function decode( b:ByteArray ):Array
		{
			var decoder:PacketDecoder = new PacketDecoder( b )
			return decoder.getValue();
		}
	}
}